package com.hanagent.utils;

import java.util.ArrayList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hanagent.model.Agentorders;
import com.hanagent.myModel.GoodStock;

public class AgentStockUtils {
	
	public static ArrayList<GoodStock> statisticsOrderGood(ArrayList<Agentorders> listAgentOrders) {
		ArrayList<GoodStock> goodStocks = new ArrayList<GoodStock>();
		for (int i = 0; i < listAgentOrders.size(); i++) {
			Agentorders agentOrders = listAgentOrders.get(i);
			String goodDatas = agentOrders.getGoodDatas();
			addOrderDatas(goodStocks, goodDatas);
		}
		
		return goodStocks;
	}
	
	public static ArrayList<GoodStock> addOrderDatas(ArrayList<GoodStock> goodStocks, String goodDatas) {
		JSONArray array = (JSONArray) JSON.parse(goodDatas);
		for (int j = 0; j < array.size(); j++) {
			JSONObject object = array.getJSONObject(j);
			int goodId = object.getIntValue("goodId");
			int count = object.getIntValue("count");
			double price = object.getDoubleValue("price");
			String name = object.getString("name");
			if (goodStocks.size()==0) {
				GoodStock goodStock = new GoodStock();
				goodStock.goodId = goodId;
				goodStock.stock = count;
				goodStock.price = price;
				goodStock.name = name;
				goodStocks.add(goodStock);
			}else {
				boolean hadGood = false;
				for (int k = 0; k < goodStocks.size(); k++) {
					GoodStock goodStock = goodStocks.get(k);
					if (goodId==goodStock.goodId) {
						goodStock.stock = goodStock.stock + count;
						hadGood = true;
						break;
					}
				}
				if (!hadGood) {
					GoodStock goodStock = new GoodStock();
					goodStock.goodId = goodId;
					goodStock.stock = count;
					goodStock.price = price;
					goodStock.name = name;
					goodStocks.add(goodStock);
				}
			}
			
		}
		return goodStocks;
	}
	
	/***
	 * 比较是否可以进货
	 * @param goodBuyStocks 进货
	 * @param goodSaleStocks 销货
	 * @return true 可以进货  false  不可进货
	 */
	public static boolean compareStock(ArrayList<GoodStock> goodBuyStocks, ArrayList<GoodStock> goodSaleStocks) {
		for (int i = 0; i < goodSaleStocks.size(); i++) {
			GoodStock goodStockSale = goodSaleStocks.get(i);
			boolean hadFindGood = false;
			for (int j = 0; j < goodBuyStocks.size(); j++) {
				GoodStock goodStockBuy = goodBuyStocks.get(j);
				if (goodStockBuy.goodId == goodStockSale.goodId) {
					hadFindGood = true;
					if (goodStockBuy.stock<goodStockSale.stock) {
						return false;
					}
				}
			}
			if (!hadFindGood) {
				return false;
			}
		}
		return true;
	}
	
	/***
	 * 计算库存
	 * @param goodBuyStocks
	 * @param goodSaleStocks
	 * @return
	 */
	public static ArrayList<GoodStock> calStock(ArrayList<GoodStock> goodBuyStocks, ArrayList<GoodStock> goodSaleStocks) {
		for (int i = 0; i < goodSaleStocks.size(); i++) {
			GoodStock goodStockSale = goodSaleStocks.get(i);
			for (int j = 0; j < goodBuyStocks.size(); j++) {
				GoodStock goodStockBuy = goodBuyStocks.get(j);
				if (goodStockBuy.goodId == goodStockSale.goodId) {
					goodStockBuy.stock = goodStockBuy.stock - goodStockSale.stock;
				}
			}
		}
		return goodBuyStocks;
	}

}






